//
//  RegistViewController.m
//  B项目
//
//  Created by lanou on 16/8/6.
//  Copyright © 2016年 Work. All rights reserved.
//

#import "RegistViewController.h"
#import "PrefixHeader.pch"
#import "comleteRegistViewController.h"
#import "YGValidator.h"
#import "MBProgressHUD.h"
#import <AVOSCloud/AVOSCloud.h>
@interface RegistViewController ()<UITextFieldDelegate>

@property(nonatomic,strong) UITextField *userTF;
@property(nonatomic,strong)UITextField *passWordUserTF;
@property(nonatomic,strong)MBProgressHUD *HUD;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
     self.navigationItem.title = @"快速注册 - 验证手机";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftItemAction)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.userTF.delegate= self;

    [self setRegist];
}

//点击返回
- (void)leftItemAction
{
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)setRegist
{
    UIView *userView = [[UIView alloc]initWithFrame:CGRectMake(20, 30, kScreenWidth - 40, 60)];

    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 45, userView.ownWidth, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    UIImageView *userImg = [[UIImageView alloc]initWithFrame:CGRectMake(0,10, 30, 30)];
    //    userImg.backgroundColor = [UIColor redColor];
    userImg.image = [UIImage imageNamed:@"phone.png"];

   self.
    userTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 10, userView.ownWidth -40, 30)];
    _userTF.placeholder = @"请输入手机号码";
    _userTF.font = [UIFont systemFontOfSize:14];
    [_userTF setBorderStyle:(UITextBorderStyleNone)];
    [userView addSubview:_userTF];
    [userView addSubview:userImg];
    [userView addSubview:lineView];
    [self.view addSubview:userView];


    UIView *passWordView = [[UIView alloc]initWithFrame:CGRectMake(20, 90, kScreenWidth - 40, 60)];
    passWordView.userInteractionEnabled = YES;

    UIView *lineViewP = [[UIView alloc]initWithFrame:CGRectMake(0, 45, userView.ownWidth, 1)];
    lineViewP.backgroundColor = [UIColor grayColor];
    UIImageView *passWordImg = [[UIImageView alloc]initWithFrame:CGRectMake(0,10, 30, 30)];
    passWordImg.image = [UIImage imageNamed:@"yanzheng.png"];

    self.passWordUserTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 10, passWordView.ownWidth -120, 30)];
    _passWordUserTF.placeholder = @"请输入验证码";
    _passWordUserTF.font = [UIFont systemFontOfSize:14];
    [_passWordUserTF setBorderStyle:(UITextBorderStyleNone)];

    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(passWordImg.ownWidth + _passWordUserTF.ownWidth, 10, 100, 30);
    [button setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(btAction) forControlEvents:(UIControlEventTouchUpInside)];

    [passWordView addSubview:lineViewP];
    [passWordView addSubview:_passWordUserTF];
    [passWordView addSubview:passWordImg];
    [passWordView addSubview:button];
    [self.view addSubview:passWordView];

    UIImageView *RegistImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 180, kScreenWidth - 40, 30)];
    RegistImg.userInteractionEnabled = YES;
    RegistImg.image = [UIImage imageNamed:@"login_tip_button@2x.png"];
    UIButton *LoaginBT = [[UIButton alloc]initWithFrame:CGRectMake((RegistImg.ownWidth -60)/2, 0, 60, 30)];
    [LoaginBT setTitle:@" 下一步" forState:(UIControlStateNormal)];
    [LoaginBT setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [LoaginBT addTarget:self action:@selector(registBTAction) forControlEvents:(UIControlEventTouchUpInside)];
    [RegistImg addSubview:LoaginBT];
    [self.view addSubview:RegistImg];

}



//返回
- (void)btAction
{
    [AVOSCloud requestSmsCodeWithPhoneNumber:self.userTF.text callback:^(BOOL succeeded, NSError *error) {

        if (succeeded) {
            NSLog(@"发送成功");
        }else{
            NSLog(@"发送失败");
        }
        // 发送失败可以查看 error 里面提供的信息
    }];


}

//点击下一步的点击事件
- (void)registBTAction
{


    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:self.userTF.text smsCode:self.passWordUserTF.text block:^(AVUser *user, NSError *error) {
        // 如果 error 为空就可以表示登录成功了，并且 user 是一个全新的用户
    }];


    comleteRegistViewController *completeRegistVC = [[comleteRegistViewController alloc]init];
    [self.navigationController pushViewController:completeRegistVC animated:YES];
    self.HUD = [[MBProgressHUD alloc]init];
    [self.view addSubview:self.HUD];
    self.HUD.dimBackground = YES;

    _HUD.mode = MBProgressHUDModeIndeterminate;
    _HUD.opacity = 0.8;
    _HUD.color = [UIColor redColor];
    _HUD.color = [_HUD.color colorWithAlphaComponent:1];
    _HUD.labelFont = [UIFont systemFontOfSize:13];
    self.HUD.labelText = @"loading...";
    _HUD.minShowTime = 5;
    _HUD.cornerRadius = 20.0;
    _HUD.animationType = MBProgressHUDAnimationZoomIn;
    [_HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];

}


//任务，测试进度显示
- (void)myProgressTask {
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        _HUD.progress = progress;
        usleep(50000);
    }
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSMutableString *currentPassWordStr = [NSMutableString stringWithString:_userTF.text];
    [currentPassWordStr replaceCharactersInRange:range withString:string];
    [YGValidator validateText:currentPassWordStr regular:@"\\w{6,16}"@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$" textField:_userTF correctImage:@"true" errorImage:@"error"];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
